package com.cityfood.controller;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.pojo.CfUser;
import com.cityfood.pojo.CfUserAddress;
import com.cityfood.service.AddressService;

@Controller
public class AddressController {
	
	@Autowired
	private AddressService addressService;
	
	/**
	 * 根据用户id查询用户收获地址
	 */
	@RequestMapping(value="/userAddress",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getAddress(@RequestParam Integer userId){
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		List<CfUserAddress> list = addressService.getAddressByUserId(userId);
		if (list==null) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("暂无地址");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		List<Map<String,Object>> ls = new ArrayList<>();
		for (CfUserAddress address : list) {
			Map<String,Object> ms = new LinkedHashMap<>();
			ms.put("addressId", address.getId());
			ms.put("consignee", address.getConsignee());
			ms.put("phone", address.getPhone());
			ms.put("province", address.getProvince());
			ms.put("city", address.getCity());
			ms.put("county", address.getCounty());
			ms.put("detailedAddress", address.getDetailedAddress());
			ms.put("address", address.getProvince()+address.getCity()+address.getCounty()+address.getDetailedAddress());
								
			ls.add(ms);
		}
		
		cityFoodResult.setCode(200);
		cityFoodResult.setMsg("查询地址成功");
		cityFoodResult.setData(ls);
		return cityFoodResult;
	}
	
	/**
	 * 添加用户收获地址
	 */
	@RequestMapping(value="/userAddress",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult saveAddress(@RequestParam Integer userId,
										@RequestParam String consignee,
										@RequestParam String phone,
										@RequestParam String province,
										@RequestParam String city,
										@RequestParam String county,
										@RequestParam String detailedAddress){
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		CfUserAddress address=new CfUserAddress();
		
		address.setUserId(userId);
		address.setConsignee(consignee);
		address.setPhone(phone);
		address.setProvince(province);
		address.setCity(city);
		address.setCounty(county);
		address.setDetailedAddress(detailedAddress);
		
		int i = addressService.insertAddress(address);
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setData(true);
			cityFoodResult.setMsg("添加地址成功");
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setData(false);
		cityFoodResult.setMsg("添加地址失败");
		return cityFoodResult;
	}
	
	/**
	 * 修改用户收获地址
	 */
	@RequestMapping(value="/userAddress",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateAddress(HttpSession session,@RequestParam Integer userId,
										@RequestParam Integer addressId,
										@RequestParam String consignee,
										@RequestParam String phone,
										@RequestParam String province,
										@RequestParam String city,
										@RequestParam String county,
										@RequestParam String detailedAddress){
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		CfUserAddress address=new CfUserAddress();
		address.setId(addressId);
		address.setUserId(userId);
		address.setConsignee(consignee);
		address.setPhone(phone);
		address.setProvince(province);
		address.setCity(city);
		address.setCounty(county);
		address.setDetailedAddress(detailedAddress);
		
		int i = addressService.updayeAddress(address);
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setData(true);
			cityFoodResult.setMsg("修改地址成功");
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setData(false);
		cityFoodResult.setMsg("修改地址失败");
		return cityFoodResult;
	}
	
	/**
	 * 删除用户收获地址
	 */
	@RequestMapping(value="/userAddress",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult updateAddress(HttpSession session,@RequestParam Integer addressId){
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		int i = addressService.deleteAddress(addressId);
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setData(true);
			cityFoodResult.setMsg("删除地址成功");
			return cityFoodResult;
		}
		cityFoodResult.setCode(201);
		cityFoodResult.setData(false);
		cityFoodResult.setMsg("删除地址失败");
		return cityFoodResult;
	}
	
	
	

}
